package com.os.start.controller;

import com.os.core.common.JsonResult;
import com.os.core.common.ResultCode;
import com.os.start.pojo.SUser;
import com.os.start.service.UserService;
import com.os.utils.JqGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author songc
 * @Date 2022-01-06
 * @Desc:
 * @Version 1.0
 */
@RestController
@RequestMapping("mybatis")
public class MyBatisCrudController {

    @Autowired
    private UserService userService;

    @Autowired
    private Sid sid;

    @RequestMapping("/saveUser")
    public JsonResult saveUser(){

        String userId = sid.nextShort();
        SUser sUser = new SUser();
        sUser.setId(userId);
        sUser.setSex("南");
        sUser.setAge(29);
        sUser.setName("设计模式");

        userService.saveUser(sUser);

        return JsonResult.returnResponse(true);

    }

    @RequestMapping("/updateUser")
    public JsonResult updateUser(){

        SUser sUser = new SUser();
        sUser.setId("220106GHT06H61S8");
        sUser.setSex("南");
        sUser.setAge(15);
        sUser.setName("加减运算");

        userService.updateUser(sUser);

        return JsonResult.returnResponse(true);

    }

    @RequestMapping("/deleteUser")
    public JsonResult deleteUser(){

        userService.deleteUser("220106GHT06H61S8");

        return JsonResult.returnResponse(true);

    }

    @RequestMapping("/queryUserById")
    public JsonResult queryUserById(){
        return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, userService.queryUserById("220106GHZ4DMK58H"));
    }

    @RequestMapping("/queryUserList")
    public JsonResult queryUserList() throws InterruptedException {

        SUser user = new SUser();
        user.setName("计");
        return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, userService.queryUserList(user));
    }

    @RequestMapping("/queryUserListPaged")
    public JsonResult queryUserListPaged(Integer page){
        if(page == null){
            page = 1;
        }
        int pageSize = 3;

        SUser user = new SUser();
        user.setName("计");
        List<SUser> list = userService.queryUserListPaged(user, page, pageSize);
        return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, list);
    }

    @RequestMapping("/queryUserListPagedJqGrid")
    public JsonResult queryUserListPagedJqGrid(Integer page){
        if(page == null){
            page = 1;
        }
        int pageSize = 3;

        SUser user = new SUser();
        user.setName("计");
        JqGridResult grid = userService.queryUserListPagedJqGrid(user, page, pageSize);
        return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, grid);
    }

    @RequestMapping("/queryUserInfoById")
    public JsonResult queryUserInfoById(){
        List<SUser> list = userService.queryUserListCustom("220106GHXR5N80ZC");
        if(list != null && !list.isEmpty()){
            return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, list);
        }else{
            return JsonResult.returnResponse(true, ResultCode.SERVICE_OK, null);
        }
    }

    @RequestMapping("/testTransactional")
    public JsonResult testTransactional(){
        SUser user = new SUser();
        user.setId(sid.nextShort());
        user.setName("马桶台");
        user.setAge(20);
        user.setSex("女");

        userService.saveUserTransactional(user);

        return JsonResult.returnResponse(true);
    }


}
